//
//  NewFeatureViewController.swift
//  cxd4iphone
//
//  Created by hexy on 12/25/15.
//  Copyright © 2015 hexy. All rights reserved.
//

import UIKit

private struct kConstraints {
    
    static let reuseId: String = "NewFeatureCellreuseId"
    static let itemCount: Int = 4
}

class NewFeatureViewController: UICollectionViewController {

    init() {
        
        super.init(collectionViewLayout: UICollectionViewFlowLayout())
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.whiteColor()

        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: kConstraints.reuseId)
        
        makeLayout()
    }
    
    private func makeLayout() {
        
        let layout = collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.itemSize = CGSizeMake(xx_screenWidth(), xx_screenHeight())
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
    }



    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return kConstraints.itemCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(kConstraints.reuseId, forIndexPath: indexPath) as! NewFeatureCell
    
        cell.imageIndex = indexPath.item
        
        return cell
    }

    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        let indexPath = collectionView.indexPathsForVisibleItems().last!
        
        if indexPath.item == kConstraints.itemCount - 1 {
            
            let cell = collectionView.cellForItemAtIndexPath(indexPath) as! NewFeatureCell
            cell.showJoinNowBtn()
        }
    }
}
private class NewFeatureCell: UICollectionViewCell {
    
    private var imageIndex: Int = 0 {
        
        didSet {
            
            joinNowBtn.hidden = true
            imageView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        makeUI()
    }


    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func makeUI() {
        
        addSubview(imageView)
        
        imageView.frame = bounds
        
        addSubview(joinNowBtn)
        joinNowBtn.snp_makeConstraints { (make) -> Void in
            
            make.height.equalTo(xx_height(40))
            make.width.equalTo(xx_width(190))
            make.centerX.equalTo(self)
            make.bottom.equalTo(self).offset(xx_height(-50))
        }
    }
    
    
    private func showJoinNowBtn() {
        
        joinNowBtn.hidden = false
        
        joinNowBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1.2, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            
            self.joinNowBtn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                
        }
    }
    
    @objc private func joinNow() {
        
        NSNotificationCenter.defaultCenter().postNotificationName(Define.chooseRootViewControllerNotification, object: NewFeatureViewController.self)
    }
    
    private lazy var imageView = UIImageView()
    
    private lazy var joinNowBtn: UIButton = {
        
        let btn = UIButton(title: "立 即 加 入", titleColor: UIColor.whiteColor(), titleSize: 20, bgColor: UIColor.clearColor())
        
        btn.titleLabel?.font = xx_boldFontOfSize(size: 20)
        btn.layer.borderWidth = 1
        btn.layer.borderColor = UIColor.whiteColor().CGColor
        btn.addTarget(self, action: "joinNow", forControlEvents: .TouchUpInside)
        
        return btn
    }()

}
